package com.yonglian.system.service.impl;

import java.util.Date;
import java.util.List;
import com.yonglian.common.utils.DateUtils;
import com.yonglian.common.utils.ShiroUtils;
import com.yonglian.system.mapper.CmsProjectBaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yonglian.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.yonglian.system.domain.CmsProjectBase;
import com.yonglian.system.mapper.CmsProjectTypeMapper;
import com.yonglian.system.domain.CmsProjectType;
import com.yonglian.system.service.ICmsProjectTypeService;
import com.yonglian.common.core.text.Convert;

/**
 * 项目类型Service业务层处理
 * 
 * @author gyok
 * @date 2024-05-28
 */
@Service
public class CmsProjectTypeServiceImpl implements ICmsProjectTypeService 
{
    @Autowired
    private CmsProjectTypeMapper cmsProjectTypeMapper;

    @Autowired
    private CmsProjectBaseMapper cmsProjectBaseMapper;

    /**
     * 查询项目类型
     * 
     * @param id 项目类型主键
     * @return 项目类型
     */
    @Override
    public CmsProjectType selectCmsProjectTypeById(Long id)
    {
        return cmsProjectTypeMapper.selectCmsProjectTypeById(id);
    }

    /**
     * 查询项目类型列表
     * 
     * @param cmsProjectType 项目类型
     * @return 项目类型
     */
    @Override
    public List<CmsProjectType> selectCmsProjectTypeList(CmsProjectType cmsProjectType)
    {
        return cmsProjectTypeMapper.selectCmsProjectTypeList(cmsProjectType);
    }

    /**
     * 新增项目类型
     * 
     * @param cmsProjectType 项目类型
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCmsProjectType(CmsProjectType cmsProjectType)
    {
        cmsProjectType.setCreateTime(DateUtils.getNowDate());
        cmsProjectType.setCreateBy(ShiroUtils.getSysUser().getUserName());
        int rows = cmsProjectTypeMapper.insertCmsProjectType(cmsProjectType);
        insertCmsProjectBase(cmsProjectType);
        return rows;
    }

    /**
     * 修改项目类型
     * 
     * @param cmsProjectType 项目类型
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCmsProjectType(CmsProjectType cmsProjectType)
    {
        Long[] idList = cmsProjectType.getCmsProjectBaseList() != null ?
                cmsProjectType.getCmsProjectBaseList().stream().filter(p -> p.getId() != null).map(p -> p.getId()).toArray(Long[]::new)
                : new Long[0];
        cmsProjectBaseMapper.deleteCmsProjectBaseByNotInList(cmsProjectType.getId(), idList);
        insertCmsProjectBase(cmsProjectType);
        return cmsProjectTypeMapper.updateCmsProjectType(cmsProjectType);
    }

    /**
     * 批量删除项目类型
     * 
     * @param ids 需要删除的项目类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCmsProjectTypeByIds(String ids)
    {
        cmsProjectBaseMapper.deleteCmsProjectBaseAllChildrenByProjectTypeIds(Convert.toStrArray(ids));
        return cmsProjectTypeMapper.deleteCmsProjectTypeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除项目类型信息
     * 
     * @param id 项目类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCmsProjectTypeById(Long id)
    {
        cmsProjectBaseMapper.deleteCmsProjectBaseByProjectTypeId(id);
        return cmsProjectTypeMapper.deleteCmsProjectTypeById(id);
    }

    /**
     * 新增项目基础信息信息
     * 
     * @param cmsProjectType 项目类型对象
     */
    public void insertCmsProjectBase(CmsProjectType cmsProjectType)
    {
        List<CmsProjectBase> cmsProjectBaseList = cmsProjectType.getCmsProjectBaseList();
        if (StringUtils.isNull(cmsProjectBaseList)) {
            return;
        }
        Long typeId = cmsProjectType.getId();
        List<CmsProjectBase> insertList = cmsProjectBaseList.stream().filter(p -> p.getId() == null).map(p -> {
            p.setProjectTypeId(typeId);
            p.setCreateTime(new Date());
            p.setCreateBy(ShiroUtils.getSysUser().getUserName());
            return p;
        }).toList();
        if (!insertList.isEmpty())
        {
            cmsProjectBaseMapper.batchInsertCmsProjectBase(insertList);
        }
        List<CmsProjectBase> updateList = cmsProjectBaseList.stream().filter(p -> p.getId() != null).map(p -> {
            p.setProjectTypeId(typeId);
            return p;
        }).toList();
        if (!updateList.isEmpty()) {
            cmsProjectBaseMapper.batchUpdateCmsProjectBase(updateList);
        }
    }
}
